<?php
defined ( 'BASEPATH' ) or exit ( 'No direct script access allowed' );
date_default_timezone_set ( 'prc' );
class Comment extends CI_Controller {
	public function __construct() {
		parent::__construct ();
		if (! $this->session->userdata ( 'logged_in' )) {
			redirect ( 'passport/index' );
			exit ();
		}
	}
	/*
	 *返回评价列表页面
	 */
	public function index() {
		$this->load->library ( 'pagination' );
		// 每页显示数据条数
		$per_page=20;//暂定为20条
		
		$config['base_url']=site_url('comment/index');
		$this->load->model ( 'comment_model' );
		// 数据总数
		$config ['total_rows'] = $this->comment_model->count_comments();
		//$config ['total_rows']=200;
		$config ['per_page'] = $per_page;
		$config ['first_link'] = '第一页';
		$config ['last_link'] = '最后一页';
		$config ['next_link'] = '下一页';
		$config ['prev_link'] = '上一页';
		$config['uri_segment'] =3;
		
		$this->pagination->initialize($config);
		$data['links']=$this->pagination->create_links();
		//偏移量
		$offset=intval($this->uri->segment(3));
		$data ['comments'] = $this->comment_model->get_comments ($per_page,$offset);
		$data ['title'] = '评价管理 - '.SYSTEM_NAME;
		$this->load->view ( 'header', $data );
		$this->load->view ( 'comment/index' );
		$this->load->view ( 'footer' );
	}
	/*
	 * 返回回复评价页面
	 */
	public function reply() {
		$id = $this->uri->segment(4);
		$this->load->model ( 'comment_model' );
		$data ['comment'] = $this->comment_model->load ( $id );
		$data ['title'] = '回复评价 - '.SYSTEM_NAME;
		$this->load->view ( 'header', $data );
		$this->load->view ( 'comment/reply' );
		$this->load->view ( 'footer' );
	}
	/*
	 *保存回复信息
	 */
	public function save() {
		$id = $this->input->post('id');
		$comment = array (
				'reply' => $this->input->post ( 'content' )
		);
		$this->load->model ( 'comment_model' );
		if (!empty($id)) {
			if($this->comment_model->update ( $id, $comment )){
				return_json(TRUE,'','回复成功！'); 
			}else{
				return_json(FALSE,'','回复失败！');
			}
		} 
	}
	/*
	 * 删除评价信息
	 */
	public function delete() {
	    $id = $this->input->post('id');
		$this->load->model ( 'comment_model' );
		if ($this->comment_model->delete ( $id )) {
			return_json(TRUE,'','删除成功！'); 
	    }else{
	    	return_json(FALSE,'','删除失败！');
	    }
    }
}